
clear all
use "./longbase3.dta"
set type double
xtset cnum time

* ayear: JFY (Japanese Fiscal Year)
gen amonth = L3.month
replace amonth = 10 if year == 1996 & month == 1
replace amonth = 11 if year == 1996 & month == 2
replace amonth = 12 if year == 1996 & month == 3
gen ayear = L3.year
replace ayear = 1995 if year == 1996 & month == 1
replace ayear = 1995 if year == 1996 & month == 2
replace ayear = 1995 if year == 1996 & month == 3

* Get exchange rate from jpylcc
merge 1:1 cnum time using jpylcc
drop if _merge ==2
drop _merge
rename c jpylcc

********************** GET DATA **********************************
* Target pork HS
local pork ///
20311030 20311040 20312021 20312022 20312025 20319021 20319022 20319024 20319025 ///
20321030 20321040 20322021 20322022 20322023 20322024 20322025 20329021 20329022 ///
20329023 20329024 20329025
* pork = (carcass meat)
local carcass 20311030 20311040 20321030 20321040
local meat 20312021 20312022 20312025 20319021 20319022 20319024 20319025 ///
           20322021 20322022 20322023 20322024 20322025 20329021 20329022 ///
           20329023 20329024 20329025
* Other pork HS for quotas
local others ///
20649091 20649092 20649094 20649099 21011010 21011020 21012010 21012020 21019010 21019020 ///
160241011 160241019 160241090 160242011 160242019 160242090 160249210 160249220 160249290
* HS for FTA quotas
local Mexico ///
20312021 20312022 20312025 20319021 20319022 20319024 20319025 20322021 20322022 ///
20322023 20322024 20322025 20329021 20329022 20329023 20329024 20329025 20649094 20649092 ///
20649099 21011010 21011020 21012010 21012020 21019010 21019020 ///
160241011 160241019 160242011 160242019 160249210 160249220 
local Australia ///
20311030 20311040 20312021 20312022 20312025 20319021 20319022 20319024 20319025 ///
20321030 20321040 20322021 20322022 20322023 20322024 20322025 20329021 20329022 ///
20329023 20329024 20329025 20649091 ///
160241011 160241019 160241090 160242011 160242019 160242090 160249210 160249220 160249290
local Chile ///
20319021 20319022 20319024 20319025 20322021 20322022 20322023 20322024 20322025 ///
20329021 20329022 20329023 20329024 20329025 20649092 20649094 20649099 ///
160241011 160241019 160241090 160242011 160242019 160242090 160249210 160249220 160249290
local Peru ///
20322021 20322022 20322023 20322024 20322025 20329021 20329022 20329023 20329024 20329025

* Obtain kjpy kgrm (Note: All3 contains monthly cumulative data)
foreach s of local pork{
gen hscode = `s'
merge 1:1 cnum time hscode using All3, keepusing(kg kjpy)
drop if _merge == 2
gen ckjpy`s' = kjpy
gen ckgrm`s' = kg
drop kjpy kg hscode _merge
      replace ckgrm`s' = 0 if missing(ckgrm`s')
	  replace ckjpy`s' = 0 if missing(ckjpy`s')
* Generate the frame
gen td`s' =0
gen kgrm`s' =0
gen kjpy`s' =0
gen cif`s' =0
* Un-cumulate ckgrm ckjpy
sort cnum time
replace kgrm`s' = ckgrm`s' - L.ckgrm`s'
replace kgrm`s' = ckgrm`s' if month == 1
replace kjpy`s' = ckjpy`s' - L.ckjpy`s'
replace kjpy`s' = ckjpy`s' if month == 1
replace cif`s' = kjpy`s'/kgrm`s'*1000 // unit value [JPY/kg]
}
drop ckgrm* ckjpy*

foreach s of local others{
gen hscode = `s'
merge 1:1 cnum time hscode using All3, keepusing(kg)
drop if _merge == 2
gen ckgrm`s' = kg
drop kg hscode _merge
      replace ckgrm`s' = 0 if missing(ckgrm`s')
* Generate the frame
gen kgrm`s' =0
* Un-cumulate ckgrm 
sort cnum time
replace kgrm`s' = ckgrm`s' - L.ckgrm`s'
replace kgrm`s' = ckgrm`s' if month == 1
}
drop ckgrm*

* Cumulative quantity for quota (Monthly cumulate from April to March in tons)
* Australia
gen Australia_q =0
foreach v of local Australia{
* cumulate April to March
bysort ayear cnum (amonth) : gen ackgrm`v' = sum(kgrm`v')
replace Australia_q = Australia_q + ackgrm`v'/1000
}
drop ackgrm*
* Chile
gen Chile_q =0
foreach v of local Chile{
* cumulate April to March
bysort ayear cnum (amonth) : gen ackgrm`v' = sum(kgrm`v')
replace Chile_q = Chile_q + ackgrm`v'/1000
}
drop ackgrm*
* Mexico
gen Mexico_q =0
foreach v of local Mexico{
* cumulate April to March
bysort ayear cnum (amonth) : gen ackgrm`v' = sum(kgrm`v')
replace Mexico_q = Mexico_q + ackgrm`v'/1000
}
drop ackgrm*
* Peru
gen Peru_q =0
foreach v of local Peru{
* cumulate April to March
bysort ayear cnum (amonth) : gen ackgrm`v' = sum(kgrm`v')
replace Peru_q = Peru_q + ackgrm`v'/1000
}
drop ackgrm*

* Baseline Tariff Duty (Gate Price System)
* 2000-
local yy  = 2000
local av  = 4.3/100 // ad valorem tariff
local std = 546.53 // floor price
local gat = 524.00 // gate price
local dut = 482.00 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'            if cif`s' <= `bnd' &                  year >= `yy'
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & year >= `yy'
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' &                  year >= `yy'
}
foreach s of local carcass{
replace td`s' = `dut'*0.75         if cif`s' <= `bnd'*0.75 &                       year >= `yy'
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & year >= `yy'
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 &                       year >= `yy'
}

* SAFEGUARD2
* 2001m8-2002m3(time499-506) 2002m8-2003m3(511-518) 2003m8-2004m3(523-530) 2004m8-2005m3(535-542) 
* gat:524->653 std:546.53->681.8 wikipedia sagaku-kanzei
local av  = 4.3/100 // ad valorem tariff
local std = 681.8 // floor price
local gat = 653 // gate price
*local bnd = 64.53  // boundary price
local dut = 482.00 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'            if cif`s' <= `bnd' & ///
((time >= 499 & time <= 506)|(time >= 511 & time <= 518)|(time >= 523 & time <= 530)|(time >= 535 & time <= 542))
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & ///
((time >= 499 & time <= 506)|(time >= 511 & time <= 518)|(time >= 523 & time <= 530)|(time >= 535 & time <= 542))
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' & ///
((time >= 499 & time <= 506)|(time >= 511 & time <= 518)|(time >= 523 & time <= 530)|(time >= 535 & time <= 542))
}
foreach s of local carcass{
replace td`s' = `dut'*0.75         if cif`s' <= `bnd'*0.75 &   ///
((time >= 499 & time <= 506)|(time >= 511 & time <= 518)|(time >= 523 & time <= 530)|(time >= 535 & time <= 542))
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & ///
((time >= 499 & time <= 506)|(time >= 511 & time <= 518)|(time >= 523 & time <= 530)|(time >= 535 & time <= 542))
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 &  ///
((time >= 499 & time <= 506)|(time >= 511 & time <= 518)|(time >= 523 & time <= 530)|(time >= 535 & time <= 542))
}

* 1999
local yy  = 1999
local av  = 4.4/100 // ad valorem tariff
local std = 559.73 // floor price
local gat = 536.14 // gate price
*local bnd = 63.56  // boundary price
local dut = 496.17 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'            if cif`s' <= `bnd' &                  year == `yy'
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & year == `yy'
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' &                  year == `yy'
}
foreach s of local carcass{
replace td`s' = `dut'*0.75         if cif`s' <= `bnd'*0.75 &                       year == `yy'
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & year == `yy'
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 &                       year == `yy'
}
* 1998
local yy  = 1998
local av  = 4.5/100 // ad valorem tariff
local std = 572.95 // floor price
local gat = 548.28 // gate price
*local bnd = 62.62  // boundary price
local dut = 510.33 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'            if cif`s' <= `bnd' &                  year == `yy'
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & year == `yy'
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' &                  year == `yy'
}
foreach s of local carcass{
replace td`s' = `dut'*0.75         if cif`s' <= `bnd'*0.75 &                       year == `yy'
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & year == `yy'
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 &                       year == `yy'
}
* 1997
local yy  = 1997
local av  = 4.7/100 // ad valorem tariff
local std = 586.76 // floor price
local gat = 560.42 // gate price
*local bnd = 62.62  // boundary price
local dut = 524.50 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'            if cif`s' <= `bnd' &                  year == `yy'
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & year == `yy'
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' &                  year == `yy'
}
foreach s of local carcass{
replace td`s' = `dut'*0.75         if cif`s' <= `bnd'*0.75 &                       year == `yy'
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & year == `yy'
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 &                       year == `yy'
}
* 1996 
local yy  = 1996
local av  = 4.8/100 // ad valorem tariff
local std = 600.03 // floor price
local gat = 572.55 // gate price
*local bnd = 61.36  // boundary price
local dut = 538.67 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'            if cif`s' <= `bnd' &                  year == `yy'
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & year == `yy'
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' &                  year == `yy'
}
foreach s of local carcass{
replace td`s' = `dut'*0.75         if cif`s' <= `bnd'*0.75 &                       year == `yy'
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & year == `yy'
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 &                       year == `yy'
}

* SAFEGUARD1 (alic)
* 1996m7-1997m3 (6.4% std=755.09, gat=709.67)
* 1996 
local av  = 6.4/100 // ad valorem tariff
local std = 755.09 // floor price
local gat = 709.67 // gate price
local dut = 538.67 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'             if cif`s' <= `bnd' & (time >= 438 & time <=446)
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & (time >= 438 & time <=446)
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' & (time >= 438 & time <=446)
}
foreach s of local carcass{
replace td`s' = `dut'*0.75          if cif`s' <= `bnd'*0.75 & (time >= 438 & time <=446)
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & (time >= 438 & time <=446)
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 & (time >= 438 & time <=446)
}
*1997m4-1997m6 (4.7% std:728.19, gat=695.50) 
* 1997
local av  = 4.7/100 // ad valorem tariff
local std = 728.19 // floor price
local gat = 695.50 // gate price
local dut = 524.50 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'             if cif`s' <= `bnd' & (time >= 447 & time <=449)
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & (time >= 447 & time <=449)
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' & (time >= 447 & time <=449)
}
foreach s of local carcass{
replace td`s' = `dut'*0.75          if cif`s' <= `bnd'*0.75 & (time >= 447 & time <=449)
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & (time >= 447 & time <=449)
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 & (time >= 447 & time <=449)
}
* 1996 -March
local yy  = 1996
local av  = 4.9/100 // ad valorem tariff
local std = 613.34 // floor price
local gat = 584.69 // gate price
*local bnd = 60.51  // boundary price
local dut = 552.83 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach s of local meat{
replace td`s' = `dut'            if cif`s' <= `bnd' &                  year == `yy' & month <= 3
replace td`s' = `std' - cif`s'    if cif`s' >= `bnd' & cif`s' <= `gat' & year == `yy' & month <= 3
replace td`s' = cif`s'*(`av'/1)   if cif`s' >= `gat' &                  year == `yy' & month <= 3
}
foreach s of local carcass{
replace td`s' = `dut'*0.75         if cif`s' <= `bnd'*0.75 &                       year == `yy' & month <= 3
replace td`s' = `std'*0.75 - cif`s' if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 & year == `yy' & month <= 3
replace td`s' = cif`s'*(`av'/1)     if cif`s' >= `gat'*0.75 &                       year == `yy' & month <= 3
}

* Quotas Mexico meat 2005-2020
local country = "Mexico"
local av  = 2.2/100 // ad valorem tariff
local std = 535.53 // floor price
local gat = 524 // gate price
*local bnd = 64.53  // boundary price
local dut = 482 // tariff duty
local bnd = `gat' - `dut'  // boundary price
foreach ay in 2020 2019 2018 2017 2016 2015{
local quota = cond(`ay' == 2020,1,0)*90000 /// 
			+ cond(`ay' == 2019,1,0)*90000 ///
			+ cond(`ay' == 2018,1,0)*90000 ///
			+ cond(`ay' == 2017,1,0)*90000 ///	
			+ cond(`ay' == 2016,1,0)*90000 ///	
			+ cond(`ay' == 2015,1,0)*90000
foreach s of local meat{
replace td`s' = min(`dut',(`std' - cif`s'))        if cif`s' <= `bnd' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = `std' - cif`s'                     if cif`s' >= `bnd' & cif`s' <= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = max(cif`s'*(`av'),(`std' - cif`s')) if                  cif`s' >= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
}
}
foreach ay in 2014 2013 2012 2011 2010 2009 2008 2007 2006 2005{
local quota = cond(`ay' == 2014,1,0)*90000 /// 
			+ cond(`ay' == 2013,1,0)*86000 ///
			+ cond(`ay' == 2012,1,0)*83000 ///
			+ cond(`ay' == 2011,1,0)*80000 ///	
			+ cond(`ay' == 2010,1,0)*80000 ///	
			+ cond(`ay' == 2009,1,0)*80000 ///
			+ cond(`ay' == 2008,1,0)*74000 ///
			+ cond(`ay' == 2007,1,0)*65000 ///
			+ cond(`ay' == 2006,1,0)*53000 ///
			+ cond(`ay' == 2005,1,0)*38000
foreach s of local meat{
replace td`s' = `dut'         if cif`s' <= `bnd' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = `std' - cif`s' if cif`s' >= `bnd' & cif`s' <= `gat' /// //gat = std/1.022
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = cif`s'*(`av')  if                  cif`s' >= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
}
}

* Quotas Chile meat 2007-2020
local country = "Chile"
local av  = 2.2/100 // ad valorem tariff
local std = 535.53 // floor price
local gat = 524 // gate price
*local bnd = 64.53  // boundary price
local dut = 482 // tariff duty
local bnd = `gat' - `dut'  // boundary price

foreach ay in 2020 2019 2018 2017 2016 2015{
local quota = cond(`ay' == 2020,1,0)*60000 /// 
			+ cond(`ay' == 2019,1,0)*60000 ///
			+ cond(`ay' == 2018,1,0)*60000 ///
			+ cond(`ay' == 2017,1,0)*60000 ///	
			+ cond(`ay' == 2016,1,0)*60000 ///	
			+ cond(`ay' == 2015,1,0)*60000
foreach s of local meat{
replace td`s' = min(`dut',(`std' - cif`s'))        if cif`s' <= `bnd' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = `std' - cif`s'                     if cif`s' >= `bnd' & cif`s' <= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = max(cif`s'*(`av'),(`std' - cif`s')) if                  cif`s' >= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
}
}
foreach ay in 2014 2013 2012 2011 2010 2009 2008 2007 2006 2005{
local quota = cond(`ay' == 2014,1,0)*60000 /// 
			+ cond(`ay' == 2013,1,0)*60000 ///
			+ cond(`ay' == 2012,1,0)*60000 ///
			+ cond(`ay' == 2011,1,0)*60000 ///	
			+ cond(`ay' == 2010,1,0)*52250 ///	
			+ cond(`ay' == 2009,1,0)*45500 ///
			+ cond(`ay' == 2008,1,0)*38750 ///
			+ cond(`ay' == 2007,1,0)*16000 
foreach s of local meat{
replace td`s' = `dut'         if cif`s' <= `bnd' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = `std' - cif`s' if cif`s' >= `bnd' & cif`s' <= `gat' /// //gat = std/1.022
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = cif`s'*(`av')  if                  cif`s' >= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
}
}

* Quotas Peru meat 2011-2020
local country = "Peru"
local av  = 2.2/100 // ad valorem tariff
local std = 535.53 // floor price
local gat = 524 // gate price
*local bnd = 64.53  // boundary price
local dut = 482 // tariff duty
local bnd = `gat' - `dut'  // boundary price

foreach ay in 2020 2019 2018 2017 2016 2015{
local quota = cond(`ay' == 2020,1,0)*5000 /// 
			+ cond(`ay' == 2019,1,0)*5000 ///
			+ cond(`ay' == 2018,1,0)*5000 ///
			+ cond(`ay' == 2017,1,0)*5000 ///	
			+ cond(`ay' == 2016,1,0)*5000 ///	
			+ cond(`ay' == 2015,1,0)*5000
foreach s of local meat{
replace td`s' = min(`dut',(`std' - cif`s'))        if cif`s' <= `bnd' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = `std' - cif`s'                     if cif`s' >= `bnd' & cif`s' <= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = max(cif`s'*(`av'),(`std' - cif`s')) if                  cif`s' >= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
}
}
foreach ay in 2014 2013 2012 2011{
local quota = cond(`ay' == 2014,1,0)*4000 /// 
			+ cond(`ay' == 2013,1,0)*3000 ///
			+ cond(`ay' == 2012,1,0)*2000 ///
			+ cond(`ay' == 2011,1,0)*83 	
foreach s of local meat{
replace td`s' = `dut'         if cif`s' <= `bnd' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = `std' - cif`s' if cif`s' >= `bnd' & cif`s' <= `gat' /// //gat = std/1.022
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = cif`s'*(`av')  if                  cif`s' >= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
}
}

* Quotas Australia meat and carcass 2014-2020
local country = "Australia"
local av  = 2.2/100 // ad valorem tariff
local std = 535.53 // floor price
local gat = 524 // gate price
*local bnd = 64.53  // boundary price
local dut = 482 // tariff duty
local bnd = `gat' - `dut'  // boundary price

foreach ay in 2020 2019 2018 2017 2016 2015 2014{
local quota = cond(`ay' == 2020,1,0)*14000 /// 
			+ cond(`ay' == 2019,1,0)*14000 ///
			+ cond(`ay' == 2018,1,0)*12320 ///
			+ cond(`ay' == 2017,1,0)*10640 ///	
			+ cond(`ay' == 2016,1,0)*8960  ///	
			+ cond(`ay' == 2015,1,0)*7280  ///
			+ cond(`ay' == 2014,1,0)*933
foreach s of local meat{
replace td`s' = min(`dut',(`std' - cif`s'))        if cif`s' <= `bnd' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = `std' - cif`s'                     if cif`s' >= `bnd' & cif`s' <= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = max(cif`s'*(`av'),(`std' - cif`s')) if                  cif`s' >= `gat' ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
}
foreach s of local carcass{
replace td`s' = min(`dut'*0.75,(`std'*0.75 - cif`s'))   if cif`s' <= `bnd'*0.75 ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = `std'*0.75 - cif`s'                     if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
replace td`s' = max(cif`s'*(`av'),(`std'*0.75 - cif`s')) if cif`s' >= `gat'*0.75 ///
	& ayear == `ay' & cnames == "`country'" & `country'_q <= `quota'
}
}

* CPTPP(2020 2019) EU(2020 2019) USA(2020)
* 2020
local ay  = 2020
local av  = 1.7/100 // ad valorem tariff
local std = 524*1.017 // floor price
local gat = 524 // gate price
*local bnd = 64.53  // boundary price
local dut = 125 // tariff duty
local bnd = `gat' - `dut'  // boundary price

foreach s of local meat{
replace td`s' = min(`dut',(`std' - cif`s'))  if cif`s' <= `bnd' ///
	& ayear == `ay' & (regime =="CPTPP" | regime =="EU" | cnames =="USA")
replace td`s' = min(`dut',(`std' - cif`s'))  if cif`s' >= `bnd' & cif`s' <= `gat' ///
	& ayear == `ay' & (regime =="CPTPP" | regime =="EU" | cnames =="USA")
replace td`s' = cif`s'*(`av')                if cif`s' >= `gat' ///
    & ayear == `ay' & (regime =="CPTPP" | regime =="EU" | cnames =="USA")
}
foreach s of local carcass{
replace td`s' = min(`dut'*0.75,(`std'*0.75 - cif`s'))  if cif`s' <= `bnd'*0.75 ///
	& ayear == `ay' & (regime =="CPTPP" | regime =="EU" | cnames =="USA")
replace td`s' = min(`dut'*0.75,(`std'*0.75 - cif`s'))  if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 ///
	& ayear == `ay' & (regime =="CPTPP" | regime =="EU" | cnames =="USA")
replace td`s' = cif`s'*(`av')                if cif`s' >= `gat'*0.75 ///
    & ayear == `ay' & (regime =="CPTPP" | regime =="EU" | cnames =="USA")
}
local ay  = 2019
local av  = 1.9/100 // ad valorem tariff
foreach s of local meat{
replace td`s' = min(`dut',(`std' - cif`s'))  if cif`s' <= `bnd' ///
	& ayear == `ay' & regime =="CPTPP"
replace td`s' = min(`dut',(`std' - cif`s'))  if cif`s' >= `bnd' & cif`s' <= `gat' ///
	& ayear == `ay' & regime =="CPTPP"
replace td`s' = cif`s'*(`av')                if cif`s' >= `gat' ///
    & ayear == `ay' & regime =="CPTPP"
}
foreach s of local carcass{
replace td`s' = min(`dut'*0.75,(`std'*0.75 - cif`s'))  if cif`s' <= `bnd'*0.75 ///
	& ayear == `ay' & regime =="CPTPP"
replace td`s' = min(`dut'*0.75,(`std'*0.75 - cif`s'))  if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 ///
	& ayear == `ay' & regime =="CPTPP"
replace td`s' = cif`s'*(`av')                if cif`s' >= `gat'*0.75 ///
    & ayear == `ay' & regime =="CPTPP"
}
local av = 2.0/100
foreach s of local meat{
replace td`s' = min(`dut',(`std' - cif`s'))  if cif`s' <= `bnd' ///
	& ayear == `ay' & regime =="EU"
replace td`s' = min(`dut',(`std' - cif`s'))  if cif`s' >= `bnd' & cif`s' <= `gat' ///
	& ayear == `ay' & regime =="EU"
replace td`s' = cif`s'*(`av')                if cif`s' >= `gat' ///
    & ayear == `ay' & regime =="EU"
}
foreach s of local carcass{
replace td`s' = min(`dut'*0.75,(`std'*0.75 - cif`s'))  if cif`s' <= `bnd'*0.75 ///
	& ayear == `ay' & regime =="EU"
replace td`s' = min(`dut'*0.75,(`std'*0.75 - cif`s'))  if cif`s' >= `bnd'*0.75 & cif`s' <= `gat'*0.75 ///
	& ayear == `ay' & regime =="EU"
replace td`s' = cif`s'*(`av')                if cif`s' >= `gat'*0.75 ///
    & ayear == `ay' & regime =="EU"
}

*** Drop others
foreach s of local others{
drop kgrm`s' 
}
* Tariff　Duty Revenue (each meat)
foreach s of local pork{
gen tdrev`s' = kgrm`s'*td`s' // JPY : td [jpy/kgrm]
}
* Strip the bone of carcass
foreach s of local carcass{
replace kgrm`s' = kgrm`s'*0.75
}

* All meat tariff duty revenue from (country time)
egen tdrev_all = rowtotal(tdrev*) //JPY
	bysort cnum year (month) : gen ytdrev_all = sum(tdrev_all) // cumulate 12 months
* All meat weight imported (country time)
egen kgrm_all = rowtotal(kgrm*)  // OK because we dropped kgrm for "others"
	bysort cnum year (month) : gen ykgrm_all = sum(kgrm_all) // cumulate 12 months
* All meat value imported (country time)
egen kjpy_all = rowtotal(kjpy*)
	bysort cnum year (month) : gen ykjpy_all = sum(kjpy_all) // cumulate 12 months
*** Drop kgrm2* kjpy2*
drop kgrm2* kjpy2*
drop namae Australia* Chile* Mexico* Peru*
drop td2* tdrev2*

* Unit value (country time) 
gen cif = kjpy_all/kgrm_all*1000 // JPY/kg
	gen ycif = ykjpy_all/ykgrm_all*1000 // JPY/kg
* Tariff duty (country time)
gen td = tdrev_all/kgrm_all // JPY/kg
	gen ytd = ytdrev_all/ykgrm_all // JPY/kg
* Price
gen pr = cif + td
	gen ypr = ycif + ytd
* Tariff rates
gen tr = td/(cif)
	gen ytr = ytd/(ycif)

********************************************************************************
* Dummy variables
tabulate time, generate(t)
tabulate cnum, generate(c)
xtset cnum time
* Instrumental Variables 
gen lnjpylcc = ln(jpylcc)
bysort cnum year (month): gen yjpylcc = sum(jpylcc)
gen lnyjpylcc = ln(yjpylcc)
********************************************************************************
*drop if cif > 800 // low (basic): Pork(1)=porkR regular
drop if cif < 800 // high: Pork(2)=porkP premium
********************************************************************************
********************************************************************************
* Share
gen kjpy_dem = pr*kgrm_all/1000
egen csum = sum(kjpy_dem), by(time) // country-sum
*	egen ycsum = sum(ykjpy_all), by(time) // country-sum
gen lnsh = ln(kjpy_dem/csum)
*	gen lnysh = ln(ykjpy_dem/ycsum)
gen lnpr = ln(pr)
*	gen lnypr = ln(ypr)
********************************************************************************
/*
********************************************************************************
* cif > 800 (porkL = Pork(1))
* LS
log using porkR_sigma_LS.log
xtreg lnsh t1-t300 lnpr, vce(robust) fe //
log close
* IV
log using porkR_sigma_IV.log
xtivreg2 lnsh t1-t300 (lnpr=lnjpylcc yjpylcc), robust bw(5) fe endog(lnpr) //
log close
* DELTA METHOD
log using porkR_index.log
*nlcom (sigma: 1-_b[lnpr])
forvalues i = 1/300{
nlcom (pindx: exp(-((0)+_b[t`i'])/_b[lnpr]))
}
log close


********************************************************************************
*/
********************************************************************************
* cif < 800 (porkH = Pork(2))
xtivreg2 lnsh t1-t300 (lnpr=lnjpylcc), fe endog(lnpr) // IV not relevant
gen lnce = ln(cif) - lnjpylcc
gen lncif = ln(cif)
xtset cnum time
xtreg lnce lnjpylcc, fe // NOT relevant => No feedback
xtreg lnce lnjpylcc, vce(robust) fe   // NOT relevant => NO feedback
* Use FE(LS)
xtreg lnsh t1-t300 lnpr, vce(robust) fe // For all cif > 800
* DELTA METHOD
nlcom (sigma: 1-_b[lnpr])
********************************************************************************
